package com.mutouren.modules.datapush.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mutouren.common.orm.BaseDaoImpl;
import com.mutouren.common.orm.IbatisHelper;
import com.mutouren.modules.datapush.DatabaseAlias;
import com.mutouren.modules.datapush.base.PushMessage;
import com.mutouren.modules.datapush.base.PushStatus;
import com.mutouren.modules.datapush.dao.PushMessageDao;

public class PushMessageDaoImpl  extends BaseDaoImpl<PushMessage> implements PushMessageDao {
	private static final String NAMESPACE = PushMessageDao.class.getName() + ".";

	@Override
	public String getNameSpace(String statement) {
		return NAMESPACE + statement;
	}

	@Override
	public String getCfgName() {
		return DatabaseAlias.Datapush.value;
	}

	@Override
	public int updateProcessing(PushMessage message) {		
		message.setPushStatus(PushStatus.PROCESSING);
		int num = IbatisHelper.update(getNameSpace("updateProcessing"), message, getCfgName());
		if (num != 1) {
			throw new RuntimeException("RemotePushServer updateProcessing fatal error: " + message.getId());
		}
		message.setVersion(message.getVersion() + 1);
		return num;
	}

	@Override
	public int updateResult(PushMessage message) {
		int num = IbatisHelper.update(getNameSpace("updateResult"), message, getCfgName());
		if (num != 1) {
			throw new RuntimeException("RemotePushServer updateResult fatal error: " + message.getId());
		}
		message.setVersion(message.getVersion() + 1);
		return num;		
	}
	
	@Override
	public int updateFinish(PushMessage message) {
		message.setIsFinish(true);
		return IbatisHelper.update(getNameSpace("updateFinish"), message, getCfgName());	
	}	

	@Override
	public List<PushMessage> selectByCreateTime(Date createTime, int size) {
		Map<String, Object> mapParam = new HashMap<String, Object>();
		mapParam.put("createTime", createTime);
		mapParam.put("size", size);		
		return IbatisHelper.selectList(getNameSpace("selectByCreateTime"), mapParam, getCfgName());
	}

	@Override
	public List<PushMessage> selectById(long id, int size) {
		Map<String, Object> mapParam = new HashMap<String, Object>();
		mapParam.put("id", id);
		mapParam.put("size", size);			
		return IbatisHelper.selectList(getNameSpace("selectById"), mapParam, getCfgName());
	}

	@Override
	public void insert(List<PushMessage> listMessage) {
		IbatisHelper.insert(getNameSpace("insertBatch"), listMessage, getCfgName());
	}


	
}
